package base;

import java.awt.EventQueue;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Scanner;

/**
 * Simulate the behavior of different LoadBalancer
 * 
 * @author rweiller
 * 
 */
public class Simulator
{
	public Source				src;

	// Config file
	public static final String	CONFIG_FILE		= "trunk/src/config";
	public static Properties	prop;

	//Results file
	public static String				RESULTS			= "id;weight;in;out;\n";
	public static String				RESULTS_FILE	= "trunk/results/results.csv";
	public FileWriter			fstream;
	public BufferedWriter		out;

	/**
	 * Retrieve Integer property in the configuration file
	 * 
	 * @param name a property name
	 * @return the correspondent value
	 */
	public static int getIntProperty(String name)
	{
		return Integer.parseInt(prop.getProperty(name));
	}

	/**
	 * Retrieve property in the configuration file
	 * 
	 * @param name a property name
	 * @return the correspondent string
	 */
	public static String getProperty(String name)
	{
		return prop.getProperty(name);
	}

	/**
	 * Launch the Simulator.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			@Override
			public void run()
			{
				try
				{
					System.out.println("-- Program start --");
					System.out.println("Tape 'Y/N' if you want to generate source");

					Simulator sim = new Simulator();

					Scanner sc = new Scanner(System.in);
					String s = sc.nextLine();
					if (s.compareToIgnoreCase("y") == 0)
					{
						System.out.print("Generating source element...");
						sim.src = new Source(Simulator.getIntProperty("source.rd.seed"));
						System.out.println("ok");

						System.out.print("Writing in file " + RESULTS_FILE + "...");
						sim.writeSource(RESULTS_FILE);
						System.out.println("ok");
					}
					/*new ServerAdministrator(Simulator.getIntProperty("servers.number"),
											Simulator.getIntProperty("servers.power"));*/

					if (!new File(RESULTS_FILE).exists())
					{
						System.out.println("\nProgram abort : please generate source before processing.");
					}
					else if (new File(RESULTS_FILE).length() == 0)
					{
						System.out.println("\nProgram abort : results file is empty.");
					}
					else
					{
						System.out.println("\nProgram ended successfully.");
					}

					System.exit(0);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the Simulator.
	 * 
	 * @throws IOException
	 */
	public Simulator() throws IOException
	{
		initialize();
	}

	/**
	 * Generate a LoadBalancer, depending on the config file
	 * 
	 * @param choice correspond to sim.loadbalancer
	 * @param nb_server correspond to sim.servers
	 * @return loadbalancer used by simulator
	 */
	/*public LoadBalancer generateLoadBalancer(int choice, int nb_server)
	{
		LoadBalancer lb = null;
		switch (choice)
		{
			case 1:
				lb = new LoadBalancer1();
				break;
			case 2:
				lb = new LoadBalancer2();
				break;
			case 3:
				lb = new LoadBalancer3();
				break;
			case 4:
				lb = new LoadBalancer4();
				break;
			case 5:
				lb = new LoadBalancer5();
				break;
			case 6:
				lb = new LoadBalancer6();
				break;
			case 7:
				lb = new LoadBalancer7();
				break;
			case 8:
				lb = new LoadBalancer8();
				break;
			default:
				System.err.println("Config file : sim.loadbalancer have to be between 1 - 8 inclusive");
				break;
		}
		return lb;
	}*/

	/**
	 * Generate and return the source, depending on the source choice
	 * 
	 * @param source a choice between 1 and 5 inclusive
	 * @return source used by simulator
	 * @throws IOException
	 */
	//	private Source generateSource(int source) throws IOException
	//	{
	//		Source src = null;
	//
	//		if (source > 5 || source < 1)
	//		{
	//			throw new IOException("Config file : sim.source have to be between 1 and 5 inclusive");
	//		}
	//
	//		src = new Source(	getProperty("sim.source." + source + ".seed"),
	//							getProperty("sim.source.range"),
	//							getProperty("sim.source.sample"));
	//
	//		return src;
	//	}

	/**
	 * Initialize Simulator.
	 * 
	 * @throws IOException
	 */
	private void initialize() throws IOException
	{
		loadConfig();
	}

	/**
	 * Load the configuration file
	 */
	public void loadConfig()
	{
		try
		{
			InputStream is = new FileInputStream(CONFIG_FILE);
			prop = new Properties();
			prop.load(is);
		}
		catch (FileNotFoundException e)
		{
			System.out.println("Configuration file not found.");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void writeSource(String filename) throws IOException
	{
		fstream = new FileWriter(filename);
		out = new BufferedWriter(fstream);
		for (Element el : src.getElementList())
		{
			out.write(el.getId() + ";" + el.getWeight() + ";" + el.getDateIn() + ";" + el.getDateOut() + ";\n");
		}
		out.close();
	}
}
